<?php
function msg($r, $msg = '', $data = [])
{
    if ($r and !$msg) {
        $msg = '操作成功';
    }
    if (!$r and !$msg) {
        $msg = '操作失败';
    }
    return json_encode([
        'code' => (int)$r,
        'msg' => $msg,
        'data' => $data,
    ]);
}

function mydtrim($arr)
{
    if (is_array($arr)) {
        foreach ($arr as $k => $v) {
            $arr[$k] = mydtrim($v);
        }
    } else {
        $arr = trim($arr);
    }
    return $arr;
}

function myswitch($kvalue, $array)
{
    return isset($array[$kvalue]) ? $array[$kvalue] : '';
}

function timeswitch($ts, $format = 'Y-m-d', $default = '')
{
    return $ts ? date($format, $ts) : $default;
}

function uniq()
{
    mt_srand();
    return md5(time() . rand(100000, 999999));
}

function load_setting($moduleid)
{
    $setting_file = APPPATH . "settings/{$moduleid}.php";
    $setting = array();
    if (file_exists($setting_file)) {
        $setting = include($setting_file);
    }
    return $setting;
}

function dhtmlspecialchars($string)
{
    if (is_array($string)) {
        return array_map('dhtmlspecialchars', $string);
    } else {
        $string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
        return str_replace('&amp;', '&', $string);
    }
}

function kv_builder($arr, $id, $name, $add = array(), $prefix = array())
{
    $res = array();
    if (count($prefix)) {
        foreach ((array)$prefix as $v) {
            $res[$v[0]] = $v[1];
        }
    }
    foreach ((array)$arr as $v) {
        $res[$v[$id]] = $v[$name];
    }
    if (count($add)) {
        foreach ((array)$add as $v) {
            $res[$v[0]] = $v[1];
        }
    }
    return $res;
}

function ka_builder($arr, $id, $name = '*', $add = array())
{
    $res = array();
    $name_arr = explode(',', $name);
    foreach ((array)$arr as $v) {
        if ($name_arr[0] == '*') {
            $res[$v[$id]] = $v;
        } else {
            $tmp = array();
            foreach ($v as $kk => $vv) {
                if (in_array($kk, $name_arr)) {
                    $tmp[$kk] = $vv;
                }
            }
            $res[$v[$id]] = $tmp;
        }
    }
    if (count($add)) {
        foreach ((array)$add as $v) {
            $res[$v[0]] = $v;
        }
    }
    return $res;
}

function kc_builder($arr, $name, $add = array())
{
    $res = array();
    foreach ((array)$arr as $v) {
        $res[] = $v[$name];
    }
    if (count($add)) {
        foreach ((array)$add as $v) {
            $res[] = $v;
        }
    }
    return $res;
}

function ks_builder($arr, $id, $name = '*', $sep = '-', $add = array())
{
    $res = array();
    $name_arr = explode(',', $name);
    foreach ((array)$arr as $v) {
        if ($name_arr[0] == '*') {
            $res[$v[$id]] = implode($sep, $v);
        } else {
            $tmp = array();
            foreach ($v as $kk => $vv) {
                if (in_array($kk, $name_arr)) {
                    $tmp[$kk] = $vv;
                }
            }
            $res[$v[$id]] = implode($sep, $tmp);
        }
    }
    if (count($add)) {
        foreach ((array)$add as $v) {
            $res[$v[0]] = $v[1];
        }
    }
    return $res;
}

function post($url, $post_data)
{
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
    //执行命令
    $data = curl_exec($curl);
    //关闭URL请求
    curl_close($curl);
    //显示获得的数据
    return $data;
}